﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace LRS.DAL
{
    public static class dSalesOrderDetail
    {
        public static List<eSalesOrderDetail> GetSalesOrderDetailByID(int OrderID)
        {
            SqlDataReader reader = SqlHelper.ExecuteReader("Select * from SalesOrderDetail where SalesOrderID=" + OrderID);
            List<eSalesOrderDetail> list = new List<eSalesOrderDetail>();
            while (reader.Read())
            {
                eSalesOrderDetail detail = new eSalesOrderDetail();
                detail.Quantity = Int32.Parse(reader["Quantity"].ToString());
                detail.ProductID = Int32.Parse(reader["ProductID"].ToString());
                detail.TotalAmount = float.Parse(reader["TotalAmount"].ToString());
                detail.AddedFee = float.Parse(reader["AddedFee"].ToString());
                detail.Description = reader["Description"].ToString();
                detail.SalesOrderID = OrderID;
                list.Add(detail);
            }
            reader.Close();
            return list;
        }
        public static void Insert(eSalesOrderDetail detail)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@SalesOrderID", detail.SalesOrderID));
            parameters.Add(new SqlParameter("@ProductID", detail.ProductID));
            parameters.Add(new SqlParameter("@Quantity", detail.Quantity));
            parameters.Add(new SqlParameter("@TotalAmount", detail.TotalAmount));
            parameters.Add(new SqlParameter("@AddedFee", detail.AddedFee));
            parameters.Add(new SqlParameter("@Description", detail.Description));
            SqlHelper.ExecuteNonQuery("Insert into SalesOrderDetail values(@SalesOrderID, @ProductID, @Quantity, @Description, @TotalAmount, @AddedFee)", parameters);
            SqlHelper.ExecuteNonQuery("Update product set Quantity = Quantity-@Quantity where ProductID=@ProductID", parameters);
        }
        public static void Update(eSalesOrderDetail detail)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@SalesOrderID", detail.SalesOrderID));
            parameters.Add(new SqlParameter("@ProductID", detail.ProductID));
            parameters.Add(new SqlParameter("@Quantity", detail.Quantity));
            parameters.Add(new SqlParameter("@TotalAmount", detail.TotalAmount));
            parameters.Add(new SqlParameter("@AddedFee", detail.AddedFee));
            parameters.Add(new SqlParameter("@Description", detail.Description));

            SqlHelper.ExecuteNonQuery("Update SalesOrderDetail set Description=@Description, AddedFee=@AddedFee WHERE SalesOrderID=@SalesOrderID AND ProductID=@ProductID", parameters);
            SqlHelper.ExecuteNonQuery("Update Product set Quantity=(Quantity + " + detail.Quantity + ") where ProductID=" + detail.ProductID);
        }
        public static void DeleteDetail(int SalesOrderID)
        {
            SqlHelper.ExecuteNonQuery("Delete from SalesOrderDetail where SalesOrderID=" + SalesOrderID);
        }
    }
}
